package com.zhizhi.interceptor;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zhizhi.common.anno.NoNeedLogin;
import com.zhizhi.domain.RequestTokenBO;
import com.zhizhi.domain.ResponseDTO;
import com.zhizhi.module.synthesizes.account.service.AccountService;
import com.zhizhi.util.RequestTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description [ 登录拦截器 ]
 * @Date 2020/7/21 11:15
 * @Author Mr.Yu
 */
//@Component
public class PmstAuthenticationInterceptor extends HandlerInterceptorAdapter {
//    private static final String TOKEN_NAME = "token";
//    @Autowired
//    private AccountService accountService;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        this.crossDomainConfig(response);
//        boolean isHandlerMethod = handler instanceof HandlerMethod;
//        if (!isHandlerMethod) {
//            return true;
//        }
//
//        // 无需登录的接口
//        boolean noNeedLogin = ObjectUtil.isNotEmpty(((HandlerMethod) handler).getMethodAnnotation(NoNeedLogin.class));
//        if (noNeedLogin) {
//            return true;
//        }
//
//        //放行的Uri前缀
//        String uri = request.getRequestURI();
//        String contextPath = request.getContextPath();
//
//        // token校验（为规范请求，只获取请求头中的token）
//        String token = request.getHeader(TOKEN_NAME);
//        if (StrUtil.isBlank(token)) {
//            // TODO: 此处错误编码需要再重新思考一下合理性
//            outputNotLoginResult(response, ResponseDTO.notLogin());
//            return false;
//        }
//        // 获取当前用户
//        RequestTokenBO requestTokenBO = accountService.getAccountByToken(token);
//        if (ObjectUtil.isEmpty(requestTokenBO)) {
//            outputNotLoginResult(response, ResponseDTO.forbidden());
//            return false;
//        }
//        RequestTokenUtil.setAccount(request, requestTokenBO);
//        return true;
//    }
//
//    private void outputNotLoginResult(HttpServletResponse response, ResponseDTO<Object> responseDTO) throws IOException {
//        String msg = JSONUtil.toJsonStr(responseDTO);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().write(msg);
//        response.flushBuffer();
//    }
//
//    /**
//     * 配置跨域
//     *
//     * @param response
//     */
//    private void crossDomainConfig(HttpServletResponse response) {
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
//        response.setHeader("Access-Control-Expose-Headers", "*");
//        response.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, " + "Accept, x-access-token ,token");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setHeader("Pragma", "no-cache");
//    }

}
